package org.droid.campusorder.action;

import java.io.StringReader;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.droid.campusorder.bean.BaseBean;
import org.droid.campusorder.bean.RespShopsBean;
import org.droid.campusorder.bean.ShopsBean;
import org.droid.campusorder.manager.NetManager;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;

import android.content.Context;
import android.os.Handler;

public class RespGetShopsAction extends Action {
	Context c;
	public RespGetShopsAction(Context c) {
		super(c);
		this.c = c;
	}

	@Override
	public BaseBean getMsgFromNetwork(Handler handler, String url,
			Hashtable<String, String> hs) throws Exception {
		SAXParserFactory factory = SAXParserFactory.newInstance();
		SAXParser Parser = factory.newSAXParser();
		XmlHandlerBase dh = new GetShopsHandler(c);
		String result = NetManager.getInstance(c).dopostAsString(
				url,hs);
		System.out.println("GetShops return:" + result);
		
		XMLReader reader = Parser.getXMLReader();
		// 为XMLReader设置内容处理器
		reader.setContentHandler(dh);
		// 开始解析文件
		reader.parse(new InputSource(new StringReader(result)));

		RespShopsBean rsb = (RespShopsBean) dh.getResult();
		if (rsb.getReturnCode() == 1000){
			System.out.println("Shops list size："+rsb.getShopsBeanList().size());
			return rsb;
		}else {
			return null;
		}
	}

	@Override
	public BaseBean getMsgFromDB(String url, Hashtable<String, String> hs)
			throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

}
	class GetShopsHandler extends XmlHandlerBase {
		RespShopsBean rsb;
		ShopsBean sb;
		String currentTag = null;
		List<ShopsBean> list;
		
		public GetShopsHandler(Context c) {
			super(c);
		}

		@Override
		public void characters(char[] ch, int start, int length)
				throws SAXException {
			if (currentTag != null) {
				String value = new String(ch, start, length);
				if (currentTag.equals("status")) {
					if (value.equals("1000")){
						list = new ArrayList<ShopsBean>();
					}
					rsb.setReturnCode(Integer.parseInt(value));
				} else if (currentTag.equals("total")){
					rsb.setTotal(Integer.parseInt(value));
				} else if (currentTag.equals("page")){
					rsb.setPage(Integer.parseInt(value));
				} else if (currentTag.equals("id")){
					sb.setId(Integer.parseInt(value));
				} else if (currentTag.equals("name")){
					sb.setName(value);
				} else if (currentTag.equals("desc")){
					sb.setDesc(value);
				} else if (currentTag.equals("phone")){
					sb.setPhone(Integer.parseInt(value));
				} else if (currentTag.equals("pic")){
					sb.setPic(value); 
				}
			}
		}

		@Override
		public void endElement(String uri, String localName, String qName)
				throws SAXException {
			currentTag = null;
			if (localName.equals("shop")){
				list.add(sb);
				sb = null;
			} else if (localName.equals("resp")){
				rsb.setShopsBeanList(list);
			}
		}

		@Override
		public void startElement(String uri, String localName, String qName,
				Attributes attributes) throws SAXException {
			currentTag = localName;
			if (localName.equals("resp")) {
				rsb = new RespShopsBean();
			} else if (localName.equals("shop")) {
				sb = new ShopsBean();
			}
		}

		@Override
		public BaseBean getResult() {
			return rsb;
		}

		@Override
		public boolean storeDataBean(BaseBean dataObject) {
			return false;
		}
		
	} 
